<?php

App::uses('AppController', 'Controller');

/**
 * Comments Controller
 *
 * @property Comment $Comment
 * @property PaginatorComponent $Paginator
 */
class CommentsController extends AppController
{

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');

    /**
     * index method
     *
     * @return void
     */
    public function index()
    {
        $this->Comment->recursive = 0;
        $this->set('comments', $this->Paginator->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */

    /**
     * add method
     *
     * @return void
     */
    public function add()
    {
        $this->autoRender = false;
        if ($this->request->is('ajax'))
        {
            $data = $this->request->data;
            $data['comment'] = strip_tags(nl2br(trim($data['comment'])));
            if ($this->Comment->save($data))
            {
                $name = strip_tags(trim($data['who']));
               
                $this->set('name',$name);
                $this->loadModel('Order');
                $sql = "UPDATE orders SET number_comment = number_comment + 1 WHERE id = {$data['orders_id']}";
                $this->Order->query($sql);
                echo 'ok';
            } else
            {
                echo 'error';
            }
        }

    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null)
    {
        if (!$this->Comment->exists($id))
        {
            throw new NotFoundException(__('Invalid comment'));
        }
        if ($this->request->is(array('post', 'put')))
        {
            if ($this->Comment->save($this->request->data))
            {
                $this->Session->setFlash(__('The comment has been saved.'));
                return $this->redirect(array('action' => 'index'));
            } else
            {
                $this->Session->setFlash(__('The comment could not be saved. Please, try again.'));
            }
        } else
        {
            $options = array('conditions' => array('Comment.' . $this->Comment->primaryKey => $id));
            $this->request->data = $this->Comment->find('first', $options);
        }
        $orders = $this->Comment->Order->find('list');
        $this->set(compact('orders'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null)
    {
        $this->Comment->id = $id;
        if (!$this->Comment->exists())
        {
            throw new NotFoundException(__('Invalid comment'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Comment->delete())
        {
            $this->Session->setFlash(__('The comment has been deleted.'));
        } else
        {
            $this->Session->setFlash(__('The comment could not be deleted. Please, try again.'));
        }
        return $this->redirect(array('action' => 'index'));
    }

}
